<?php

    addLog();

function curl($url, $isGetCookie = false) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if($isGetCookie) curl_setopt($ch, CURLOPT_HEADER, 1);
    elseif(isset($_COOKIE['loginInfo'])) {
        $cookie = $_COOKIE['loginInfo'];
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    $json = curl_exec($ch);
    curl_close($ch);
    return $json;
}

function writeCache($filename, $rank){
    @$fp=fopen(CACHEDIR.$filename, 'w');
    @$flag = fwrite($fp, serialize($rank));
    @fclose($fp);
    return $flag;
}

function readCache($filename) {
    @$fp=fopen(CACHEDIR.$filename, 'r');
    @$res = fgets($fp);
    @fclose($fp);
    return unserialize($res);
}

function arrayPlus($a) {
    $s = 0;
    foreach($a as $v) {
        $s += $v;
    }
    return $s;
}

function addLog() {
    GLOBAL $today;
    //添加访问记录
    define('LOGDIR', dirname(__FILE__).'/log/');
    date_default_timezone_set('Asia/Shanghai');
    $today = date('Ymd', mktime(0,0,0));
    $time = date('H:i:s');
    $filename = LOGDIR.$today;
    $log = "{$time}  {$_SERVER['REMOTE_ADDR']}  {$_SERVER['REQUEST_URI']}  {$_SERVER['HTTP_USER_AGENT']}\n";
    @$fp=fopen($filename, 'a');
    @$flag = fwrite($fp, $log);
    @fclose($fp);    
}

function jump($url = NULL, $param = NULL) {
    if(!$url) {
        $url = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'index.php';
    } elseif($param) {
        $url .= "?{$param}";    
    }
    header("Location:{$url}");
}

function getMyInfo($isLoadName = false){
    if(isset($_COOKIE['loginInfo'])) {
        $myInfo = curl('http://www.djmaxcn.com/main/GetMyInfo.asp');
        $myInfo = json_decode($myInfo);
        $str = "<a href=\"logout.php\">退出</a></span>";
        if($isLoadName) $str .= "&#160;&#160;&#160;<span>{$myInfo->DJNAME}({$myInfo->DJLEVEL})";
        return $str;
    } else {
        return "<p>┝ <a href=\"login.php\">登录</a></p>";
    }    
}

function getHead($title = '', $isTitle = true) {
    date_default_timezone_set('Asia/Shanghai');
    echo <<<HEADER
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" href="icon32.png" type="image/png" />
<title>TECHNIKA3 {$title}</title>
</head>
<body>
HEADER;
    if($isTitle) echo "<h1>TECHNIKA3 {$title}</h1><p>既非官方又非非官方</p><hr/>";
}
    
function getFoot($isNavi = true, $isHR = true) {
    $format_time = date('M d, y H:i:s');
    if($isHR) echo '<hr/>';
    if($isNavi) {
        echo "<p style=\"text-align: center;\">{$login}</p>";
        echo '<p style="text-align: center;"><a href="index.php">首页</a> | <a href="rank.php">排名</a> | <a href="state.php">机台状态</a> | <a href="bbs.php?type=n">公告</a><br /><a href="bbs.php?type=d">DJ留言板</a> | <a href="bbs.php?type=r">战队招募</a> | <a href="bbs.php?type=u">UGC留言板</a></p>';
    }
    echo "<p style=\"text-align: center;\">{$format_time}&#160;&#160;&#160;何凯 作品</p></body></html>";
}
